package com.tjetc.service.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.dao.Mapper;
import com.tjetc.domain.Carousel;
import com.tjetc.domain.Type;
import com.tjetc.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private Mapper mapper;
    @Autowired
    private StringRedisTemplate carouselRedisTemplate;
    @Autowired
    private StringRedisTemplate typeRedisTemplate;
    @Override
    public List<Carousel> findAllCarousel( )  {

        List<Carousel> allCarousel = mapper.findAllCarousel();
        ValueOperations ops = carouselRedisTemplate.opsForValue();
        ObjectMapper objectMapper = new ObjectMapper();
        if (carouselRedisTemplate.hasKey("allCarousel")){
//            System.out.println("truetruetruetruetruetruetruetrue");
            String  o = (String) ops.get("allCarousel");
            System.out.println("o = " + o);
            List<Carousel> list = null;
            try {
                list = objectMapper.readValue(o, List.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            return list;
        }else {
//            System.out.println("falsefalsefalsefalsefalsefalsefalsefalse");
            String s = null;
            try {
                s = objectMapper.writeValueAsString(allCarousel);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            ops.set("allCarousel", s);
            return allCarousel;
        }
    }

    @Override
    public List<Type> findAllType() {
        List<Type> allType = mapper.findAllType();
        ValueOperations ops = typeRedisTemplate.opsForValue();
        ObjectMapper objectMapper = new ObjectMapper();
        if (typeRedisTemplate.hasKey("type")){
//            System.out.println("truetruetruetruetruetruetruetrue");
            String  o = (String) ops.get("type");
            System.out.println("o = " + o);
            List<Type> list = null;
            try {
                list = objectMapper.readValue(o, List.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            return list;
        }else {
//            System.out.println("falsefalsefalsefalsefalsefalsefalsefalse");
            String s = null;
            try {
                s = objectMapper.writeValueAsString(allType);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            ops.set("type", s);
            return allType;
        }
    }
//    public static int[] generateUniqueRandomArray(int min, int max, int total) {
//        if (max - min + 1 < total) {
//            throw new IllegalArgumentException("范围太小，无法生成足够数量的不重复随机数");
//        }
//
//        List<Integer> numbers = new ArrayList<>();
//        for (int i = min; i <= max; i++) {
//            numbers.add(i);
//        }
//
//        Collections.shuffle(numbers);
//        int[] result = new int[total];
//        for (int i = 0; i < total; i++) {
//            result[i] = numbers.get(i);
//        }
//
//        return result;
//    }

}
